package com.kezesong.leasing.service.impl;

import com.kezesong.leasing.controller.response.GoodsColorRspBody;
import com.kezesong.leasing.controller.response.GoodsDetailsRspBody;
import com.kezesong.leasing.controller.response.GoodsListRspBody;
import com.kezesong.leasing.controller.response.GoodsRamRspBody;
import com.kezesong.leasing.entity.*;
import com.kezesong.leasing.mapper.GoodsInfoMapper;
import com.kezesong.leasing.service.*;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class goodsInfoServiceImpl implements GoodsInfoService {
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Autowired
    private GoodsColorService goodsColorService;

    @Autowired
    private GoodsRamService goodsRamService;

    @Autowired
    private GoodsLogisticsService goodsLogisticsService;

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Override
    public List<GoodsInfoEntity> getGoodsInfoList() {
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoMapper.selectAll();
        return goodsInfoEntities;
    }


    @Override
    public GoodsDetailsRspBody getGoodsDetails(int goodsId) {
        GoodsDetailsRspBody goodsDetailsRspBody = new GoodsDetailsRspBody();
        List<GoodsColorRspBody> goodsColorRspBodyList = new ArrayList<>();
        List<GoodsRamRspBody> goodsRamRspBodyList = new ArrayList<>();
        GoodsInfoEntity goodsInfoEntity = goodsInfoMapper.selectByPrimaryKey(goodsId);
        if (null != goodsInfoEntity){
            goodsDetailsRspBody.setCategoryId(goodsInfoEntity.getGoodsCategoryId());
            BeanUtils.copyProperties(goodsInfoEntity,goodsDetailsRspBody);   // set goods base info


            List<GoodsColorEntity> goodsColorList = goodsColorService.getGoodsColorList(goodsInfoEntity.getGoodsId());
            Iterator<GoodsColorEntity> iteratorColor = goodsColorList.iterator();
            while (iteratorColor.hasNext()){
                GoodsColorRspBody goodsColorRspBody = new GoodsColorRspBody();
                GoodsColorEntity next = iteratorColor.next();
                BeanUtils.copyProperties(next,goodsColorRspBody);
                goodsColorRspBodyList.add(goodsColorRspBody);
            }
            goodsDetailsRspBody.setGoodsColorList(goodsColorRspBodyList); //set color


            List<GoodsRAMEntity> goodsRAMList = goodsRamService.getGoodsRAMList(goodsInfoEntity.getGoodsId());
            Iterator<GoodsRAMEntity> iteratorRam = goodsRAMList.iterator();
            while (iteratorRam.hasNext()) {
                GoodsRamRspBody goodsRamRspBody = new GoodsRamRspBody();
                GoodsRAMEntity next = iteratorRam.next();
                BeanUtils.copyProperties(next,goodsRamRspBody);
                goodsRamRspBodyList.add(goodsRamRspBody);
            }
            goodsDetailsRspBody.setGoodsRAMList(goodsRamRspBodyList);// set ram

            GoodsLogisticsEntity goodsLogistics = goodsLogisticsService.getGoodsLogistics(goodsInfoEntity.getGoodsId());
            if (null != goodsLogistics){
                goodsDetailsRspBody.setAddress(goodsLogistics.getAddress()); //set address
            }
        }

        return goodsDetailsRspBody;

    }

    @Override
    public List<GoodsInfoEntity> getGoodsInfoList(int categoryId, int pageSize, int pageIndex) {
        Example example = new Example(GoodsInfoEntity.class);
        example.createCriteria().andEqualTo("goodsCategoryId", categoryId);
        RowBounds rowBounds = new RowBounds(pageIndex, pageSize);
        List<GoodsInfoEntity> goodsInfoEntities = goodsInfoMapper.selectByExampleAndRowBounds(example,rowBounds);
        return goodsInfoEntities;
    }

    @Override
    public int addGoodsInfo(GoodsInfoEntity goodsInfoEntity) {
        goodsInfoEntity.setCreatedTime(new Date());
        int rows = goodsInfoMapper.insert(goodsInfoEntity);
        return rows;
    }

    @Override
    public int modifyGoodsInfo(GoodsInfoEntity goodsInfoEntity) {
        goodsInfoEntity.setUpdatedTime(new Date());
        int rows = goodsInfoMapper.updateByPrimaryKey(goodsInfoEntity);
        return rows;
    }

    @Override
    public int deleteGoodsInfo(int goodId) {
        int rows = goodsInfoMapper.deleteByPrimaryKey(goodId);
        return rows;
    }


    @Override
    public List<GoodsListRspBody> getGoodsInfoShowRspList(int categoryId, int pageSize, int pageIndex) {

        List<GoodsListRspBody> goodsInfoShowRspBodyList = new ArrayList<>();
        GoodsCategoryEntity goodsCategoryEntity = goodsCategoryService.getGoodsCategoryList()
                .stream().filter(p->p.getCategoryId()==categoryId)
                .findFirst().orElse(null);

        List<GoodsInfoEntity> goodsInfoEntities = this.getGoodsInfoList(categoryId, pageSize, pageIndex);

        if (null != goodsInfoEntities && goodsInfoEntities.size()>0){
            Iterator<GoodsInfoEntity> iterator = goodsInfoEntities.iterator();
            while (iterator.hasNext()) {
                GoodsInfoEntity goodsInfoEntity = iterator.next();
                GoodsListRspBody goodsInfoShowRspBody = new GoodsListRspBody();
                goodsInfoShowRspBody.setCategoryName(goodsCategoryEntity.getCategoryName());
                goodsInfoShowRspBody.setCategoryFlag(goodsCategoryEntity.getCategoryFlag());
                BeanUtils.copyProperties(goodsInfoEntity,goodsInfoShowRspBody);   // set goods base info
                goodsInfoShowRspBodyList.add(goodsInfoShowRspBody);
            }

        }

        return goodsInfoShowRspBodyList;
    }
}
